// 全局变量
const API_BASE_URL = 'http://localhost:5000';

// 表单提交处理
document.getElementById('pptForm').addEventListener('submit', async function(e) {
    e.preventDefault();
    
    const formData = {
        visitorType: document.getElementById('visitorType').value,
        visitorName: document.getElementById('visitorName').value,
        visitorPosition: document.getElementById('visitorPosition').value,
        visitPurpose: document.getElementById('visitPurpose').value,
        duration: parseInt(document.getElementById('duration').value),
        companyFocus: document.getElementById('companyFocus').value
    };
    
    // 显示加载状态
    showLoading();
    
    try {
        const response = await fetch(`${API_BASE_URL}/generate-ppt`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(formData)
        });
        
        if (!response.ok) {
            throw new Error('生成失败');
        }
        
        const data = await response.json();
        
        // 显示预览
        showPreview(data);
        
    } catch (error) {
        console.error('Error:', error);
        alert('生成PPT失败，请稍后重试');
    }
});

// 显示加载状态
function showLoading() {
    document.getElementById('generateBtn').disabled = true;
    document.getElementById('generateBtn').innerHTML = '<span class="loading"></span> 生成中...';
    document.getElementById('previewArea').style.display = 'block';
    document.getElementById('pptPreview').innerHTML = '<p class="text-muted">正在生成PPT...</p>';
}

// 显示预览
function showPreview(data) {
    document.getElementById('generateBtn').disabled = false;
    document.getElementById('generateBtn').innerHTML = '<i class="fas fa-magic me-2"></i>生成PPT';
    
    const previewHtml = `
        <div class="slide-container">
            <h5>${data.title}</h5>
            <p class="text-muted">共 ${data.slideCount} 页幻灯片</p>
            <div class="mt-3">
                <h6>幻灯片预览：</h6>
                ${data.slides.map((slide, index) => `
                    <div class="slide-preview">
                        <strong>${index + 1}. ${slide.title}</strong>
                        <p class="text-muted small">${slide.description || ''}</p>
                    </div>
                `).join('')}
            </div>
        </div>
    `;
    
    document.getElementById('pptPreview').innerHTML = previewHtml;
    document.getElementById('downloadLink').href = `${API_BASE_URL}/download/${data.filename}`;
}

// 重置表单
function resetForm() {
    document.getElementById('pptForm').reset();
    document.getElementById('previewArea').style.display = 'none';
}

// 根据来访类型动态调整表单
document.getElementById('visitorType').addEventListener('change', function(e) {
    const visitorType = e.target.value;
    const purposeSelect = document.getElementById('visitPurpose');
    
    // 清空现有选项
    purposeSelect.innerHTML = '<option value="">请选择来访目的</option>';
    
    if (visitorType === 'leader') {
        // 公司领导的来访目的
        const leaderPurposes = [
            { value: 'inspection', text: '工作视察' },
            { value: 'guidance', text: '工作指导' },
            { value: 'meeting', text: '内部会议' }
        ];
        leaderPurposes.forEach(purpose => {
            const option = new Option(purpose.text, purpose.value);
            purposeSelect.add(option);
        });
    } else if (visitorType === 'guest') {
        // 外部客人的来访目的
        const guestPurposes = [
            { value: 'meeting', text: '商务洽谈' },
            { value: 'exchange', text: '技术交流' },
            { value: 'cooperation', text: '合作签约' },
            { value: 'visit', text: '参观访问' }
        ];
        guestPurposes.forEach(purpose => {
            const option = new Option(purpose.text, purpose.value);
            purposeSelect.add(option);
        });
    }
});
